// 处理业务逻辑的中间件，读取json文件并返回
const path = require('path')
const fileUtils = require('../utils/file_utils')
module.exports = async (ctx,next) => {
	// 获取url
	const url = ctx.request.url
	console.log(url)
	let filePath = url.replace('/api','')
	filePath = '../data' + filePath + '.json'
	filePath = path.join(__dirname,filePath)

	try{
		const ret = await fileUtils.getFileJsonData(filePath)
		ctx.response.body = ret
	} catch ( error ) {
	    const retMsg = {
	      code: 200,
	      msg: '文件不存在'
	    }
	    ctx.response.body = JSON.stringify(retMsg)
	}


	await next()
}